import * as types from '../../mutation-types'
import { delayBounceAction } from '../../../api/filters.js'

//用户信息
/// <summary>
/// 获取用户信息
/// </summary>
/// <param name="callback"></param>
/// <param name="token"></param>
/// <param name="refreshToken"></param>
/// <param name="uid"></param>
/// <returns></returns>

export const initUserInfo = ({ commit, state, rootState }, callback) => {

    delayBounceAction(300, function() {
        f7.httpGet(
            rootState.cache.api +
            rootState.request.get_user_profile, { uid: rootState.userProfile.uid },
            function(dataset) {
                //数据准备
                if (!dataset.data) dataset.data = {}

                //数据准备
                const data = dataset.data
                data.serverTime = dataset.serverTime

                //专栏数据状态入库

                commit(types.UPDATE_USER_PROFILE, data)

                if (callback) callback();
            },
            //true
        )
    })

}

//设置用户信息
/// <summary>
/// 更新用户信息
/// </summary>
/// <param name="token"></param>
/// <param name="refreshToken"></param>
/// <param name="uid"></param>
/// <param name="name">昵称</param>
/// <param name="head">头像</param>
/// <param name="comments">个性化签名</param>
/// <param name="sex">性别</param>
/// <returns></returns>

export const setUserInfo = ({ commit, state, rootState }, obj, callback) => {

    if (!obj) {
        obj = {}
    }

    obj.uid = obj.uid ? obj.uid : rootState.userProfile.uid
    obj.name = obj.name ? obj.name : rootState.userProfile.name
    obj.head = obj.head ? obj.head : rootState.userProfile.head
    obj.comments = obj.comments ? obj.comments : rootState.userProfile.comments
    obj.sex = obj.sex !== '' ? obj.sex : rootState.userProfile.sex
    obj.token = obj.token ? obj.token : rootState.userProfile.providerKey

    delayBounceAction(300, function() {
        myApp.requestPostApi(
            rootState.cache.api +
            rootState.request.set_user_profile,
            _.extend({ uid: rootState.userProfile.uid }, obj),
            function(dataset) {
                //数据准备
                if (!dataset.data) dataset.data = {}

                //数据准备
                const data = dataset.data
                data.serverTime = dataset.serverTime

                //专栏数据状态入库
                commit(types.UPDATE_USER_PROFILE, data)

                if (callback) callback();

            },
            //true
        )
    })

}

//获取关注的内容
/// <summary>
/// 获取用户关注内容
/// </summary>
/// <param name="callback"></param>
/// <param name="uid">用户id</param>
/// <param name="docType">表示搜索类型：1为用户，4为概念，5为收藏夹，6为专栏</param>
/// <param name="pageNumber">默认为0</param>
/// <param name="pageSize">默认为20</param>
/// <returns></returns>

export const getMyFocus = ({ commit, state, rootState }, obj, callback) => {

    if (!obj) {
        obj = {}
    }

    obj.docType = obj.docType ? obj.docType : 1
    obj.pageNumber = obj.pageNumber ? obj.pageNumber : 0
    obj.pageSize = obj.pageSize ? obj.pageSize : 20

    delayBounceAction(300, function() {
        f7.httpGet(
            rootState.cache.api +
            rootState.request.get_user_focus,
            _.extend({ uid: rootState.userProfile.uid }, obj),
            function(dataset) {
                //数据准备
                if (!dataset.data) dataset.data = {}

                //数据准备
                const data = dataset.data
                data.serverTime = dataset.serverTime

                //传入当前的七牛的token&domain参数
                commit(types.MY_FOUCES_CENTER, data)

                if (callback) callback();
            },
            false
        )
    })
}

/// <summary>
/// 获取用户的收藏夹列表
/// </summary>
/// <param name="callback"></param>
/// <param name="uid"></param>
/// <param name="pageNumber"></param>
/// <param name="pageSize"></param>
/// <returns></returns>
export const getUserFavorites = ({ commit, state, rootState }, obj, callback) => {

    if (!obj) {
        obj = {}
    }

    obj.uid = obj.uid ? obj.uid : rootState.userProfile.uid
    obj.docType = obj.docType ? obj.docType : 1
    obj.pageNumber = obj.pageNumber ? obj.pageNumber : 0
    obj.pageSize = obj.pageSize ? obj.pageSize : 20

    f7.httpGet(
        rootState.cache.api +
        rootState.request.get_user_favorites,
        _.extend({ uid: rootState.userProfile.uid }, obj),
        function(dataset) {
            //数据准备
            if (!dataset.data) dataset.data = {}

            //数据准备
            const data = dataset.data
            data.serverTime = dataset.serverTime

            //传入当前我的收藏列表
            commit(types.MY_FAVOURITE_LIST, data)

            if (callback) callback();
        },
        false
    )

}

/// <summary>
/// 获取用户的收藏夹详情
/// </summary>
/// <param name="callback"></param>
/// <param name="uid"></param>
/// <param name="pageNumber"></param>
/// <param name="pageSize"></param>
/// <param name="id">收藏夹id，有则表示修改收藏夹名称</param>
/// <returns></returns>
export const getUserFavoritesDetails = ({ commit, state, rootState }, obj, callback) => {

    if (!obj) {
        obj = {}
    }

    obj.id = obj.id ? obj.id : 3233 //测试收藏夹id
    obj.uid = obj.uid ? obj.uid : rootState.userProfile.uid
    obj.pageNumber = obj.pageNumber ? obj.pageNumber : 0
    obj.pageSize = obj.pageSize ? obj.pageSize : 20

    delayBounceAction(300, function() {
        f7.httpGet(
            rootState.cache.api +
            rootState.request.get_user_favorites_detail,
            _.extend({ uid: rootState.userProfile.uid }, obj),
            function(dataset) {
                //数据准备
                if (!dataset.data) dataset.data = {}

                //获取赋值
                const data = dataset.data
                data.serverTime = dataset.serverTime

                //传入当前的七牛的token&domain参数
                commit(types.MY_FOUCES_CENTER, data)

                if (callback) callback();
            },
            false
        )
    })

}

/// <summary>
/// 获取用户最近浏览
/// </summary>
/// <param name="callback"></param>
/// <param name="uid"></param>
/// <param name="pageNumber"></param>
/// <param name="pageSize"></param>
/// <returns></returns>

export const getUserBrowsed = ({ commit, state, rootState }, obj, callback) => {

    if (!obj) {
        obj = {}
    }
    obj.uid = obj.uid ? obj.uid : rootState.userProfile.uid
    obj.docType = obj.docType ? obj.docType : 1
    obj.pageNumber = obj.pageNumber ? obj.pageNumber : 0
    obj.pageSize = obj.pageSize ? obj.pageSize : 20

    f7.httpGet(
        rootState.cache.api +
        rootState.request.get_user_browsed,
        _.extend({ uid: rootState.userProfile.uid }, obj),
        function(dataset) {
            //数据准备
            if (!dataset.data) dataset.data = {}

            //数据准备
            const data = dataset.data
            data.serverTime = dataset.serverTime

            //传入当前的七牛的token&domain参数
            commit(types.MY_FOUCES_CENTER, data)

            if (callback) callback();
        },
        false
    )
}

/// <summary>
/// 获取用户草稿
/// </summary>
/// <param name="callback"></param>
/// <param name="uid"></param>
/// <param name="pageNumber"></param>
/// <param name="pageSize"></param>
/// <returns>返回用户草稿列表，草稿表示未发布的文章</returns>

export const getUserDraft = ({ commit, state, rootState }, obj, callback) => {

    if (!obj) {
        obj = {}
    }
    obj.uid = obj.uid ? obj.uid : rootState.userProfile.uid
    obj.docType = obj.docType ? obj.docType : 1
    obj.pageNumber = obj.pageNumber ? obj.pageNumber : 0
    obj.pageSize = obj.pageSize ? obj.pageSize : 20

    f7.httpGet(
        rootState.cache.api +
        rootState.request.get_user_draft,
        _.extend({ uid: rootState.userProfile.uid }, obj),
        function(dataset) {
            //数据准备
            if (!dataset.data) dataset.data = {}

            //数据准备
            const data = dataset.data
            data.serverTime = dataset.serverTime

            //传入当前的七牛的token&domain参数
            commit(types.MY_FOUCES_CENTER, data)

            if (callback) callback();
        },
        false
    )
}

/// <summary>
/// 删除用户收藏夹
/// </summary>
/// <param name="token"></param>
/// <param name="refreshToken"></param>
/// <param name="uid"></param>
/// <param name="id">收藏夹id，有则表示修改收藏夹名称</param>
/// <returns></returns>

export const getDeleteUserFavourite = ({ commit, state, rootState }, obj, callback) => {

    if (!obj) {
        obj = {}
    }
    obj.uid = obj.uid ? obj.uid : rootState.userProfile.uid
    obj.docType = obj.docType ? obj.docType : 1
    obj.pageNumber = obj.pageNumber ? obj.pageNumber : 0
    obj.pageSize = obj.pageSize ? obj.pageSize : 20

    f7.httpGet(
        rootState.cache.api +
        rootState.request.delete_user_favorites_folder,
        _.extend({ uid: rootState.userProfile.uid }, obj),
        function(dataset) {
            //数据准备
            if (!dataset.data) dataset.data = {}

            //数据准备
            const data = dataset.data
            data.serverTime = dataset.serverTime

            //传入当前的七牛的token&domain参数
            commit(types.MY_FOUCES_CENTER, data)

            if (callback) callback();
        },
        false
    )
}

//七牛上传初始化图片
export const initQiniuToken = ({ commit, state, rootState }, callback) => {

    f7.httpGet(
        state.request.get_uptoken, {},
        function(dataset) {
            //数据准备
            if (!dataset.data) dataset.data = {}

            //数据准备
            const data = dataset.data
            data.serverTime = dataset.serverTime

            const obj = {}
            Object.keys(data).forEach(k => { obj[k] = data[k] }) //提交参数

            //传入当前的七牛的token&domain参数
            // commit(types.CHANGE_CURRENT_POSITION, { obj })
            commit(types.MY_CURRENT_PARAMTERS, { obj })

            if (callback) callback();
        },
        true
    )

}

//更新应用的当前页面位置
export const myCurrentPosition = ({ commit, state }, obj) => {

    commit(types.MY_CURRENT_PARAMTERS, { obj })

}